package com.huawei.appgallery.search.ui;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import androidx.annotation.NonNull;
import com.huawei.appgallery.foundation.card.base.bean.KeywordInfo;
import com.huawei.appgallery.foundation.service.common.protocol.AppDetailActivityProtocol;
import com.huawei.appgallery.foundation.service.common.protocol.AppDetailActivityProtocol.Request;
import com.huawei.appgallery.foundation.ui.framework.cardframe.bean.BaseCardBean;
import com.huawei.appgallery.foundation.ui.framework.cardframe.fragment.TaskFragment;
import com.huawei.appgallery.search.impl.bean.AppInfoBean;
import com.huawei.appgallery.search.impl.bean.AutoCompleteResBean;
import com.huawei.appmarket.service.exposure.bean.ExposureDetail;
import com.huawei.secure.android.common.intent.SafeBundle;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import o.ccv;
import o.ccz;
import o.cdb;
import o.cdh;
import o.cfw;
import o.cii;
import o.cjk;
import o.cjm;
import o.cjo;
import o.cuv;
import o.cux;
import o.cvc;
import o.cvd;
import o.cvg;
import o.cvn;
import o.cvt;
import o.dgh;
import o.dgu;
import o.dmq;
import o.dwy;
import o.SomeTimerTask;
import o.emz;
import o.epv;
import o.CollectCheck;
import o.ActivityUtil;
import o.evt;

public class AutoCompleteFragment extends TaskFragment<AutoCompleteFragmentProtocol> implements cvg, OnScrollListener, OnLayoutChangeListener {
    private static String ˎ = "1";
    private long ʹ;
    private boolean ʻˊ;
    private cvn ʻˋ;
    private BroadcastReceiver ʻᐝ = new BroadcastReceiver(this) {
        final /* synthetic */ AutoCompleteFragment ˊ;

        {
            this.ˊ = r1;
        }

        public void onReceive(Context context, Intent intent) {
            if (this.ˊ.ॱ != null) {
                this.ˊ.ॱ.notifyDataSetChanged();
            }
        }
    };
    private boolean ʼ;
    private boolean ʼˊ;
    private StringBuilder ʼˋ;
    private List<cvc> ʽ;
    private b ˊ;
    private ListView ˋ = null;
    private String ˏ = "";
    private AutoCompleteTextAdapter ॱ = null;
    private AutoCompleteResBean ॱॱ;
    private String ᐝ = "";
    private c ﹳ;
    private cvg.b ﾞ;
    private int ﾟ = ccz.ॱ();

    class c extends SomeTimerTask {
        private int ˏ;
        final /* synthetic */ AutoCompleteFragment ॱ;

        public c(AutoCompleteFragment autoCompleteFragment, int i) {
            this.ॱ = autoCompleteFragment;
            this.ˏ = i;
        }

        public int[] getVisibleImtePos() {
            int[] iArr = new int[]{-1, -1};
            if (this.ॱ.ˋ != null) {
                iArr[0] = this.ॱ.ˋ.getFirstVisiblePosition();
                iArr[1] = this.ॱ.ˋ.getLastVisiblePosition();
            }
            return iArr;
        }

        public View ˏ(int i) {
            if (this.ॱ.ˋ == null) {
                return null;
            }
            return this.ॱ.ˋ.getChildAt(i - this.ॱ.ˋ.getFirstVisiblePosition());
        }

        public List<ExposureDetail> ˏ(int i, int i2) {
            if (this.ॱ.ˋ == null) {
                return null;
            }
            Object obj = evt.ˊ(this.ॱ.ˋ) >= 50 ? 1 : null;
            ArrayList arrayList = new ArrayList();
            if (obj == null) {
                return arrayList;
            }
            while (i <= i2) {
                if (ˏ(i) != null) {
                    AppInfoBean ˏ;
                    if (i < this.ॱ.ʽ.size()) {
                        try {
                            ˏ = ((cvc) this.ॱ.ʽ.get(i)).ˏ();
                        } catch (Exception e) {
                            cux.ˏ.ˎ("AutoCompleteFragment", "getExposeData " + e.toString());
                        }
                    } else {
                        ˏ = null;
                    }
                    if (ˏ != null) {
                        CharSequence ॱॱ = ˏ.ॱॱ();
                        if (!TextUtils.isEmpty(ॱॱ)) {
                            ArrayList arrayList2 = new ArrayList();
                            arrayList2.add(ॱॱ);
                            ExposureDetail exposureDetail = new ExposureDetail();
                            exposureDetail.ˎ(arrayList2);
                            exposureDetail.ˏ(System.currentTimeMillis());
                            arrayList.add(exposureDetail);
                        }
                    }
                }
                i++;
            }
            return arrayList;
        }

        public long ˊ() {
            return this.ॱ.ʹ;
        }

        public int ˋ() {
            return this.ˏ;
        }
    }

    class d implements OnItemClickListener {
        final /* synthetic */ AutoCompleteFragment ॱ;

        private d(AutoCompleteFragment autoCompleteFragment) {
            this.ॱ = autoCompleteFragment;
        }

        public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
            cvc o_cvc = (cvc) this.ॱ.ॱ.getItem(i);
            if (o_cvc != null) {
                AppInfoBean ˏ = o_cvc.ˏ();
                if (ˏ == null) {
                    final KeywordInfo ˊ = o_cvc.ˊ();
                    if (ˊ != null && !TextUtils.isEmpty(ˊ.ॱ())) {
                        dgh.ˋ("searchfillword_click", this.ॱ.ˊ(ˊ.ॱ(), this.ॱ.ˏ, i));
                        new Handler().post(new Runnable(this) {
                            final /* synthetic */ d ˊ;

                            public void run() {
                                if (ˊ == null) {
                                    return;
                                }
                                if (ˊ.ˏ() == 1) {
                                    this.ˊ.ॱ.ॱ(ˊ.ˋ());
                                } else if (this.ˊ.ॱ.ˊ != null) {
                                    this.ˊ.ॱ.ˊ.ॱ(ˊ.ॱ(), ˊ.ˋ(), false, true);
                                }
                            }
                        });
                        if (!TextUtils.isEmpty(ˊ.ˋ())) {
                            cfw.ˎ(dgu.ˋ("12", ˊ.ˋ(), cdh.ˎ(this.ॱ.ॱˋ())), null);
                            return;
                        }
                        return;
                    }
                    return;
                }
                dgh.ˋ("searchfillword_click", this.ॱ.ˊ(ˏ.v_(), this.ॱ.ˏ, i));
                this.ॱ.ॱ(ˏ);
            }
        }
    }

    public static AutoCompleteFragment ॱ(boolean z, String str) {
        AutoCompleteFragment autoCompleteFragment = new AutoCompleteFragment();
        autoCompleteFragment.ˋ(z, str);
        return autoCompleteFragment;
    }

    private void ˋ(boolean z, String str) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("param_key", z);
        bundle.putString("param_keyword", str);
        ʼ(bundle);
    }

    public void ˏ(Bundle bundle) {
        ॱˋ(true);
        SafeBundle safeBundle = new SafeBundle(ˊॱ());
        this.ʼˊ = safeBundle.getBoolean("param_key");
        if (TextUtils.isEmpty(this.ˏ)) {
            this.ˏ = safeBundle.getString("param_keyword");
        }
        super.ˏ(bundle);
    }

    public View ˋ(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        super.ˋ(layoutInflater, viewGroup, bundle);
        ViewGroup viewGroup2 = (ViewGroup) layoutInflater.inflate(e.ˏॱ, viewGroup, false);
        viewGroup2.setBackgroundColor(ॱˋ().getResources().getColor(o.cuv.d.ˋ));
        this.ˋ = (ListView) viewGroup2.findViewById(cuv.b.ᐝ);
        this.ॱ = new AutoCompleteTextAdapter(ॱˋ());
        this.ॱ.setSearchListener(this.ˊ);
        if (this.ˋ != null) {
            this.ˋ.setAdapter(this.ॱ);
            this.ˋ.setOnScrollListener(this);
            this.ˋ.setOnItemClickListener(new d());
            this.ʻˋ = new cvn(this.ˋ, this);
            this.ˋ.addOnLayoutChangeListener(this.ʻˋ);
        }
        if (this.ʽ != null) {
            this.ॱ.setmFilteredList(this.ʽ);
        }
        if (ʼʻ()) {
            ˈॱ();
        }
        ॱ();
        return viewGroup2;
    }

    protected void ॱ() {
        ActivityUtil.registerReceiver(ॱˋ(), new IntentFilter(ccv.ˋ()), this.ʻᐝ);
    }

    protected void ʼᐝ() {
        ActivityUtil.unregisterReceiver(ॱˋ(), this.ʻᐝ);
    }

    public void ॱ(Activity activity) {
        super.ॱ(activity);
        if (activity instanceof b) {
            this.ˊ = (b) activity;
        }
        this.ﾟ = cdh.ˎ(ॱˋ());
    }

    public void ˎ() {
        super.ˎ();
        this.ˊ = null;
        if (this.ˋ != null) {
            this.ˋ.removeOnLayoutChangeListener(this);
        }
    }

    private void ʾॱ() {
        cux.ˏ.ˎ("AutoCompleteFragment", "on refresh");
        dmq.ˏ.ॱ(new com.huawei.appgallery.foundation.ui.framework.cardframe.fragment.TaskFragment.c(this.ᶥॱ));
        ʿˊ();
    }

    public void ˋ(TaskFragment taskFragment, List<cvt> list) {
        if (!this.ʼˊ) {
            list.add(new cvd(this.ˏ, this.ﾟ));
        }
    }

    public boolean ˊ(TaskFragment taskFragment, TaskFragment.b bVar) {
        if (bVar.ˋ instanceof AutoCompleteResBean) {
            AutoCompleteResBean autoCompleteResBean = (AutoCompleteResBean) bVar.ˋ;
            ॱˋ(true);
            if (this.ʼ) {
                if (autoCompleteResBean.getRtnCode_() == 0) {
                    this.ॱॱ = autoCompleteResBean;
                    if (CollectCheck.isEmpty(this.ॱॱ.ˏ()) && CollectCheck.isEmpty(this.ॱॱ.ˊ())) {
                        if (this.ʼˋ != null) {
                            this.ʼˋ = null;
                        }
                        ʽᐝ();
                    } else if (ʾ()) {
                        ˈॱ();
                    } else {
                        this.ﾞ.ˊ(taskFragment);
                    }
                } else {
                    ʽᐝ();
                }
            }
        }
        return false;
    }

    private void ʽᐝ() {
        if (this.ˊ != null) {
            this.ˊ.ͺ();
        } else if (this.ﾞ != null) {
            this.ﾞ.ˊ();
        } else {
            cux.ˏ.ˊ("AutoCompleteFragment", "couldn't show hot page.");
        }
    }

    private void ˈॱ() {
        this.ॱ.clearCache();
        this.ॱ.setmKeyword(this.ˏ);
        this.ʼˋ = new StringBuilder();
        if (!(this.ॱॱ == null || CollectCheck.isEmpty(this.ॱॱ.ˏ()))) {
            for (AppInfoBean appInfoBean : this.ॱॱ.ˏ()) {
                if (appInfoBean != null) {
                    cvc o_cvc = new cvc();
                    o_cvc.ॱ(appInfoBean);
                    this.ॱ.addData(o_cvc);
                    ˋ(this.ʼˋ, appInfoBean.v_());
                }
            }
        }
        if (!(this.ॱॱ == null || CollectCheck.isEmpty(this.ॱॱ.ˊ()))) {
            for (KeywordInfo keywordInfo : this.ॱॱ.ˊ()) {
                if (!(keywordInfo == null || TextUtils.isEmpty(keywordInfo.ॱ()))) {
                    o_cvc = new cvc();
                    o_cvc.ˊ(keywordInfo);
                    this.ॱ.addData(o_cvc);
                    ˋ(this.ʼˋ, keywordInfo.ॱ());
                }
            }
        }
        if (!this.ॱ.isFilteredListEmpty()) {
            cux.ˏ.ˎ("AutoCompleteFragment", "onCompleted, adapter.notifyDataSetChanged()");
            this.ʽ = this.ॱ.getmFilteredList();
            this.ॱ.notifyDataSetChanged();
        }
        ʽˊ();
        this.ʻˊ = true;
    }

    private void ˋ(StringBuilder stringBuilder, String str) {
        stringBuilder.append("\"");
        stringBuilder.append(str);
        stringBuilder.append("\"");
        stringBuilder.append(",");
    }

    private void ॱ(@NonNull AppInfoBean appInfoBean) {
        cux.ˏ.ˎ("AutoCompleteFragment", "loadAppDetail");
        String ॱॱ = appInfoBean.ॱॱ();
        if (1 == appInfoBean.detailType_) {
            if (ॱˋ() != null) {
                ˏ(appInfoBean);
                ((dwy) cdb.ॱ(dwy.class)).ˏ(ॱˋ(), appInfoBean);
                return;
            }
            cux.ˏ.ˋ("AutoCompleteFragment", "context is null, can not show mini detail dialog");
        } else if (ॱॱ == null || !ॱॱ.startsWith("html")) {
            ˏ(ॱॱ);
        } else {
            int indexOf = ॱॱ.indexOf(124);
            if (indexOf != -1) {
                ॱॱ = ॱॱ.substring(indexOf + 1);
            }
            emz.ˊ(ॱˋ(), ॱॱ);
        }
    }

    private void ˏ(@NonNull AppInfoBean appInfoBean) {
        if (!TextUtils.isEmpty(appInfoBean.ˎͺ())) {
            return;
        }
        if (ˎ.equals(appInfoBean.ˈ())) {
            appInfoBean.ʿ(appInfoBean.ʿ());
        } else {
            appInfoBean.ʿ(appInfoBean.ˋॱ());
        }
    }

    private void ˏ(String str) {
        cjo appDetailActivityProtocol = new AppDetailActivityProtocol();
        appDetailActivityProtocol.ˏ(new Request(str, this.ᐝ));
        cjk.ˎ().ˋ(ॱˋ(), new cjm("appdetail.activity", appDetailActivityProtocol));
    }

    private void ॱ(String str) {
        if (!TextUtils.isEmpty(str)) {
            BaseCardBean baseCardBean = new BaseCardBean();
            baseCardBean.ॱ(str);
            if (!cii.ˊ().ˋ(ॱˋ(), baseCardBean)) {
                ˏ(str);
            }
        }
    }

    private LinkedHashMap<String, String> ˊ(String str) {
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("autowordlist", str);
        linkedHashMap.put("service_type", String.valueOf(this.ﾟ));
        return linkedHashMap;
    }

    public void ॱ(int i, String str, boolean z, cvg.b bVar) {
        this.ˏ = str;
        this.ʼˊ = z;
        this.ﾞ = bVar;
        this.ﾟ = i;
        if (TextUtils.isEmpty(str)) {
            this.ʼ = false;
            ʽᐝ();
        } else if (this.ʼ) {
            ʾॱ();
        } else {
            ॱˋ(false);
            this.ʼ = true;
            ʿˊ();
        }
    }

    private LinkedHashMap<String, String> ˊ(String str, String str2, int i) {
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("fillword", str);
        linkedHashMap.put("inputword", str2);
        linkedHashMap.put("position", String.valueOf(i));
        linkedHashMap.put("service_type", String.valueOf(this.ﾟ));
        return linkedHashMap;
    }

    public void ʽˊ() {
        this.ʹ = System.currentTimeMillis();
        this.ﹳ = new c(this, this.ﾟ);
        this.ﹳ.ˏ();
    }

    public void onScroll(AbsListView absListView, int i, int i2, int i3) {
    }

    public void onLayoutChange(View view, int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
        if (this.ʻˋ != null && this.ʻˋ.ॱ() && this.ʻˊ) {
            this.ʻˊ = false;
            ʽˊ();
        }
    }

    public void onScrollStateChanged(AbsListView absListView, int i) {
        if (i == 1 && absListView.getId() == cuv.b.ᐝ) {
            this.ʻˊ = false;
            epv.ॱ(absListView.getContext(), (View) absListView);
        } else if (i == 0) {
            ʽˊ();
        }
    }

    public void ˋᐝ() {
        super.ˋᐝ();
        if (this.ʻˋ != null) {
            this.ʻˋ.ˊ(true);
        }
    }

    public void ˎˎ() {
        super.ˎˎ();
        this.ʼ = false;
    }

    public void ᐝ() {
        super.ᐝ();
        ʼᐝ();
        if (this.ʼˋ != null && this.ʼˋ.length() > 0) {
            dgh.ˋ("250401", ˊ(this.ʼˋ.toString().substring(0, this.ʼˋ.length() - 1)));
        }
    }
}
